<?php if(!defined('CALL')) exit('No direct script access allowed');

include_once('lib/utils.class.php');
include_once('lib/xtemplate.class.php');
include_once('lib/logger.class.php');
include_once('view/v_plans.class.php');

class vEvent
{
	private static $xtpl_name = 'xtpl/main-event.xtpl';
	
	public static function template_open()
	{
		return new XTemplate(self::$xtpl_name);
	}
	
	public static function build_eventBox($event, $actor, &$xtpl = null)
	{
		$html = vPlans::build_eventBox($event, $actor->id);
		$ts = Utils::parse_dt($event["dt_beg"]);
		$txt = Utils::get_day_name($ts).", ".date("d.m.Y", $ts);
		
		if(is_null($xtpl))
			$xtpl = self::template_open();
			
		$xtpl->assign('root', ROOT_URL);
		$xtpl->assign('date', $txt);
		$xtpl->assign('event_id', $event["event_id"]);
		$xtpl->assign('event_html', $html);

		$xtpl->parse('root.event');
		$html = $xtpl->text('root.event');
		
		$xtpl->parse('root.edit_form');
		$html .= $xtpl->text('root.edit_form');

		return $html;
	}
	
	public static function build_commentLine($event, $comments, $actor, &$xtpl = null)
	{
		$html = vPlans::build_eventBox($event, $actor->id);
		
		if(is_null($xtpl))
			$xtpl = self::template_open();
			
		foreach($comments as $com)
			self::build_commentBox($com, $actor, $xtpl);
		
		$xtpl->parse('root.comment_list');
		return $xtpl->text('root.comment_list');
	}
	
	public static function build_commentBox($comment, $actor, &$xtpl = null)
	{
		if(is_null($xtpl))
			$xtpl = self::template_open();
			
		$xtpl->assign('comment_id', $comment["id"]);
		$xtpl->assign('root', ROOT_URL);
		$xtpl->assign('user_id', $comment["user_id"]);
		$xtpl->assign('avatar', $comment["face"]);
		$xtpl->assign('dt', $comment["dt"]);
		$xtpl->assign('username', $comment["username"]);
		$xtpl->assign('text', $comment["note"]);
		
		Logger::Write(__FILE__." ".__FUNCTION__." line: ".__LINE__, print_r($comment, TRUE));
		
		if(($actor->id == $comment["user_id"]) || ($actor->id == $comment["event_user_id"]))
			$xtpl->parse('root.comment_list.comment.act_del');
		
		$xtpl->parse('root.comment_list.comment');
		return $xtpl->text('root.comment_list.comment');
	}
	
	public static function build_commentForm(&$xtpl = null)
	{
		if(is_null($xtpl))
			$xtpl = self::template_open();
			
		$xtpl->parse('root.comment_list.form');
		return $xtpl->text('root.comment_list.form');
	}
	
	public static function build_userLine($event, $links, $actor, &$xtpl = null)
	{
		$html = vPlans::build_eventBox($event, $actor->id);
		
		if(is_null($xtpl))
			$xtpl = self::template_open();
			
		$ts = Utils::parse_dt($event["dt_beg"]);
		$txt = Utils::get_day_name($ts).", ".date("d.m.Y", $ts);

		$xtpl->assign('date', $txt);
		$xtpl->assign('event_id', $event["event_id"]);
		$xtpl->assign('event_html', $html);
		$xtpl->parse('root.event');
		
		$xtpl->assign('list_title', "Участники");
		foreach($links as $id=>$link)
		{
			$xtpl->assign('root', ROOT_URL);
			$xtpl->assign('user_id', $link["id"]);
			$xtpl->assign('username', $link["username"]);
			$xtpl->assign('face', $link["face"]);
			$xtpl->assign('city', $link["city"]);
			$xtpl->parse('root.user_list.item');
			
			// self::buildUserBox($id, self::$user->users[$id], $xtpl);
		}
		$xtpl->parse('root.user_list');
		return $xtpl->text('root.user_list');
	}
	
	public static function build_linkBox($link, $actor, &$xtpl = null)
	{
		if(is_null($xtpl))
			$xtpl = self::template_open();
			
		$xtpl->assign('comment_id', $link["id"]);
		$xtpl->assign('root', ROOT_URL);
		$xtpl->assign('user_id', $link["user_id"]);
		$xtpl->assign('avatar', $link["face"]);
		$xtpl->assign('dt', $link["dt"]);
		$xtpl->assign('username', $link["username"]);
		$xtpl->assign('text', $link["note"]);
		
		if(($actor->id == $link["user_id"]) || ($actor->id == $link["event_user_id"]))
			$xtpl->parse('root.comment.act_del');
		
		$xtpl->parse('root.comment');
		return $xtpl->text('root.comment');
	}
	
	public static function build_textInfo($text)
	{
		if(is_null($xtpl)) $xtpl = self::template_open();
		$xtpl->assign('text', $text);
		$xtpl->parse('root.text_info');
		return $xtpl->text('root.text_info');
	}
	
}